package com.alibaba.aone.casestudy.job;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Action;
import com.alibaba.aone.casestudy.common.entity.Casestudy;
import com.alibaba.aone.casestudy.common.entity.Failure;
import com.alibaba.aone.casestudy.common.entity.FailureTime;
import com.alibaba.aone.casestudy.common.utils.MessageUtil;

import com.alibaba.aone.casestudy.service.ActionService;
import com.alibaba.aone.casestudy.service.CasestudyService;
import com.alibaba.aone.casestudy.service.FailureService;
import com.alibaba.aone.casestudy.service.FailureTimeService;

/**
 * 类ActionJob.java的实现描述：TODO 类实现描述
 * 
 * @author yongchun.chengyc 2011-5-5 下午04:00:30
 */
public class ActionJob {
	@Autowired
	private ActionService actionService;
	@Autowired
	private CasestudyService casestudyService;
	@Autowired
	private FailureTimeService failureTimeService;

	public void job() {
		List<Action> actions = actionService.listActions();

		for (Action action : actions) {
			// 获得action的责任人
			String chargeMan = action.getChargeman();
			// 获得action的最初预计完成时间
			Date originalCompleteTime = action.getOriginalCompleteTime();
			// 获得action的实际完成时间
			Date realCompleteTime = action.getRealCompleteTime();
			// 获得当前时间
			Date now = new Date();
			// 时间差   action最初预计完成时间于当前时间的差
			int timeDiff1 = (int) ((originalCompleteTime.getTime() - now
					.getTime()) / 1000 / 60 / 60 / 24);

			
			int timeDiff2 = (int) ((now.getTime() - realCompleteTime.getTime()) / 1000 / 60 / 60 / 24);

			// 事故类和A类的action完成两周后
			int rank = getFailureRank(action);

			if ((timeDiff1 <= 2) && (timeDiff1 >= 0)) {
				
				
				/*
				 * 此处需要重构，将邮箱前缀名改为姓名
				 */

				String message = action.getChargeman() + " ,你有一个《--"
						+ action.getDescription() + "--》action需要在"
						+ action.getOriginalCompleteTime() + "前完成";
				
				MessageUtil.sendWangwangMessage(action.getChargeman(), message);
				
				System.out.println(action.getChargeman() + " ,你有一个《--"
						+ action.getDescription() + "--》action需要在"
						+ action.getOriginalCompleteTime() + "前完成");

			} else if (timeDiff1 < 0) {

				String message = action.getChargeman() + " ,你有一个《--"
						+ action.getDescription() + "--》已过期，请调整完成时间";
				MessageUtil.sendWangwangMessage(action.getChargeman(), message);
				
				
				System.out.println(action.getChargeman() + " ,你有一个《--"
						+ action.getDescription() + "--》已过期，请调整完成时间");

				String message1 = action.getFollower() + "你好 ,"
						+ action.getChargeman() + "有一个《--"
						+ action.getDescription() + "--》已过期，请跟进";
				
				MessageUtil.sendWangwangMessage(action.getFollower(), message);
				
				

				System.out.println(action.getFollower() + "你好 ,"
						+ action.getChargeman() + "有一个《--"
						+ action.getDescription() + "--》已过期，请跟进");
			}

			if ((timeDiff2 >= 14) && (timeDiff2 <= 21) && (rank == 1)
					|| (rank == 2)) {
				String message = action.getFollower() + "你好, "
				+ action.getChargeman() + "有一个《--"
				+ action.getDescription() + "--》已完成两周，请反馈效果";
		MessageUtil.sendWangwangMessage(action.getFollower() , message);
				
				System.out.println(action.getFollower() + "你好, "
						+ action.getChargeman() + "有一个《--"
						+ action.getDescription() + "--》已完成两周，请反馈效果");

			}

		}

	}

	private int getFailureRank(Action action) {
		int rank = 0;

		// 事故类和A类的action完成两周后
		String casestudyNo = action.getCasestudyNumber();

		Casestudy caseStudy = casestudyService
				.getCasestudyByNumber(casestudyNo);
		String failureNumber = caseStudy.getSourceNumber();

		List<FailureTime> failureTimes = failureTimeService
				.getFailureTime(failureNumber);

		for (FailureTime failureTime : failureTimes) {
			rank = failureTime.getRank();
		}

		return rank;
	}

}
